package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Area;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.service.IAreaService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-06-18
 */
@Controller
public class AreaController {

    private static final Logger logger = LoggerFactory.getLogger(AreaController.class);

    @Autowired
    private IAreaService areaService;

    @RequiresPermissions("admin:user:view")
    @RequestMapping("admin/area/index")
    public String index() {
        return "admin/area";
    }

    @RequiresPermissions("admin:user:view")
    @RequestMapping("/admin/area/list")
    @ResponseBody
    public Page<Area> alist() {
        Page<Area> page = areaService.selectPage(new Page<Area>(0, 10));
        return page;
    }

    @RequestMapping("/admin/area/add")
    public String add(@Valid Area area, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        area.insert();
        return "admin/area";
    }

    @RequestMapping("/admin/area/getAreaById")
    @ResponseBody
    public Area getAreaById(long id) {
        Area area = areaService.selectById(id);
        return area;
    }

    @RequestMapping("/admin/area/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = areaService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/area/changeState")
    @ResponseBody
    public String changeState(int id, int state) {
        Area area = new Area();
        area.setId(id);
        areaService.updateById(area);
        return "success";
    }

    @RequestMapping("/admin/area/modify")
    public String update(Area area) {
        boolean isSuccc = areaService.updateById(area);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    @RequestMapping("/area/list")
    @ResponseBody
    public Page<Area> list(@RequestParam(value = "ptype", required = false) String ptype) {
        if (Strings.isNullOrEmpty(ptype)) {
            ptype = "1";
        }
        Page<Area> page = new Page<Area>(0, 6);
        Wrapper wrapper = new EntityWrapper<Area>().eq("state", "1").eq("ptype", ptype);
        Page<Area> list = areaService.selectPage(page, wrapper);
        return list;
    }

    @RequestMapping("/admin/area/shi")
    @ResponseBody
    public Jinfo shi(@RequestParam(value = "code", required = false) String code,
                     @RequestParam(value = "isempty", required = false) Integer isempty) {

        logger.info("==========================/admin/area/shi isempty="+isempty);
        logger.info("==========================/admin/area/shi code="+code);
        Jinfo json = new Jinfo();
        String zhixias = "110000,120000,310000,500000";
        if (Strings.isNullOrEmpty(code)) {
            json.setCode(4);
            json.setMsg("param error");
            return json;
        }

        List<Area> shi = new ArrayList<>();
        if(isempty!=null&&isempty==1){
            Area em = new Area();
            em.setCode("");
            em.setName("请选择市区");
            shi.add(em);
        }
        String sheng = code.substring(0, 2);
        if(code!=null&&zhixias.indexOf(code)>=0){
            // 直辖市
            shi.addAll(areaService.selectList(new EntityWrapper<Area>().eq("level", 3).eq("sheng", sheng)));
        }else{
            // 非直辖
            shi.addAll(areaService.selectList(new EntityWrapper<Area>().eq("level", 2).eq("sheng", sheng)));
        }

        json.setInfo(shi);
        return json;
    }

}
